package net.wantongtech.xa.uploadbasicinfo.service;

import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.yhgl.OrgType;
import com.wtkj.vo.dzzf.share.staticvalue.AgencyType;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.constant.OrgStaticValue;
import net.wantongtech.xa.uploadbasicinfo.dao.UploadBasicInfoDao;
import net.wantongtech.xa.uploadbasicinfo.vo.Organization;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 创建中行组织结构信息服务实现
 *
 * @author MaShibo
 * 日期 :  2019/12/26
 */
@Slf4j
@Service
public class CreateBocOrganizationService {

    public static final String BOC_PARENT_ORGID = "5FDC5F93-0ED5-AD10-F3BC-04237E735646";
    public static final String DEFAULT_DESCRIPTION = "20191226";

    @Resource
    private UploadBasicInfoDao uploadBasicInfoDao;


    public void createTopOrg() throws ParseException {
        Organization organization = new Organization();
        organization.setOrgId(new RandomGUID().toString());
        organization.setPOrgId("2c90822b4d4bb56c014d4bbe2ae300000000");
        organization.setOrgType(OrgType.YH.ordinal());
        organization.setCode("6108");
        organization.setName("中国银行代发");
        organization.setDescription(DEFAULT_DESCRIPTION);
        organization.setOfficePhone("029-87602118");
        organization.setDirector("孟娅");
        organization.setStartOn(DateUtils.parseDate("2015-08-17", new String[]{"yyyy-MM-dd"}));
        organization.setEndOn(DateUtils.parseDate("2999-12-31", new String[]{"yyyy-MM-dd"}));
        organization.setIsDeleted(0);
        log.info(organization.toString());
        uploadBasicInfoDao.createValueObject(organization);
    }


    public void create() throws IOException {
        final String filePath = "C:\\Users\\MaShibo\\Desktop\\boc-net.csv";
        final String agencyId = "61010102003";
        LocalDate nowDate = LocalDate.now();
        Date startOn = nowDate.toDate();
        LocalDate endDate = LocalDate.parse("2999-12-31");
        Date endOn = endDate.toDate();

        List<Organization> organizationList = Files.readLines(new File(filePath), Charset.forName("UTF-8"), new LineProcessor<List<Organization>>() {

            private List<Organization> organizationList = Lists.newArrayList();

            private int rowNum = 1;


            @Override
            public boolean processLine(String line) {
                String[] splits = line.split(",");
                Organization organization = new Organization();
                organization.setOrgId(new RandomGUID().toString());
                organization.setPOrgId(BOC_PARENT_ORGID);
                organization.setOrgType(OrgType.ZH.ordinal());
                organization.setCode(StringUtils.deleteWhitespace(splits[1]));
                organization.setName(StringUtils.deleteWhitespace(splits[0]));
                organization.setDescription(DEFAULT_DESCRIPTION);
                organization.setOfficePhone(StringUtils.deleteWhitespace(splits[6]));
                organization.setAddress(StringUtils.deleteWhitespace(splits[4]));
                organization.setDirector(StringUtils.deleteWhitespace(splits[5]));
                organization.setIsDeleted(0);
                organization.setStartOn(startOn);
                organization.setEndOn(endOn);
                organization.setDishi(StringUtils.deleteWhitespace(splits[2]));
                organization.setCounty(StringUtils.deleteWhitespace(splits[3]));
                organization.setAgencyType(AgencyType.SERVICEHALL.getValue());
                organization.setServiceItems(OrgStaticValue.SERVICE_ITEMS);
                organization.setBusinessHours(OrgStaticValue.BUSINESS_HOURS);
                organization.setIsAnnualCard(1);
                String channelId = String.format("%s1226%s", agencyId, StringUtils.leftPad(String.valueOf(rowNum), 4, '0'));
                organization.setChannelId(channelId);
                organization.setAgentSaleState(1);
                organization.setPayReplaceState(1);
                organization.setFreeSaleState(1);
                organization.setAgentPublishState(1);
                organization.setPosInUseState(1);
                organization.setCcbMobilePayState(1);
                organizationList.add(organization);
                rowNum++;
                return true;
            }

            @Override
            public List<Organization> getResult() {
                return organizationList;
            }
        });

        log.info("共{}条网点信息", organizationList.size());
        organizationList.forEach(organization -> uploadBasicInfoDao.createValueObject(organization));
    }


}
